/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.service.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import edu.uwm.universitydb.dao.user.CreateDBUserCommand;
import edu.uwm.universitydb.dao.user.CreateUserCommand;
import edu.uwm.universitydb.dao.user.DeleteUserCommand;
import edu.uwm.universitydb.dao.user.DropDBUserCommand;
import edu.uwm.universitydb.dao.user.GetAllUsersCommand;
import edu.uwm.universitydb.dao.user.GrantAllPrivilegesCommand;
import edu.uwm.universitydb.dao.user.GrantCreateUserCommand;
import edu.uwm.universitydb.dao.user.GrantGrantPrivilageCommand;
import edu.uwm.universitydb.dao.user.SearchUserByIDCommand;
import edu.uwm.universitydb.db.connection.DBConnectionFactory;
import edu.uwm.universitydb.framework.exception.DataAccessException;
import edu.uwm.universitydb.model.Role;
import edu.uwm.universitydb.model.User;

/**
 * Service class for user implementation services
 * 
 * @author Shyam Nair
 */
public class UserServiceImpl implements UserService
{

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.service.user.UserService#createUser(edu.uwm.cs757.model.User, java.lang.String)
     */
    @Override
    public boolean createUser(final User user, final String password)
    {
        // connection
        Connection dbConnection = null;
        try
        {
            // obtain a connection from factory
            final DBConnectionFactory connectionFactory = DBConnectionFactory.getInstance();
            dbConnection = connectionFactory.getDBConnection();
            dbConnection.setAutoCommit(false);
            
            // acquire parameters
            final String userName = user.getUserName();
            final Role role = user.getRole();
            final Integer roleId = role.getRoleId();

            // create db user
            final CreateDBUserCommand dbUserCommand = new CreateDBUserCommand(userName, password);
            dbUserCommand.executeCommand(dbConnection);

            // grant privileges
            final GrantAllPrivilegesCommand dbGrantAllPrivilages = new GrantAllPrivilegesCommand(userName);
            dbGrantAllPrivilages.executeCommand(dbConnection);
            
            // grant create user
            final GrantCreateUserCommand dbGrantCreateUser = new GrantCreateUserCommand(userName);
            dbGrantCreateUser.executeCommand(dbConnection);
            
            // grant grant privilate
            final GrantGrantPrivilageCommand dbGrantPrivilage = new GrantGrantPrivilageCommand(userName);
            dbGrantPrivilage.executeCommand(dbConnection);
            
            // create an entry to user table
            final CreateUserCommand userCommand = new CreateUserCommand(userName, roleId);
            userCommand.executeCommand(dbConnection);

            // commit the transaction
            dbConnection.commit();

            return true;

        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        catch (final SQLException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        finally
        {
            if (dbConnection != null)
            {
                try
                {
                    dbConnection.close();
                }
                catch (final SQLException e)
                {
                    // DO NOTHING
                    e.printStackTrace();
                }
            }
        }
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.service.user.UserService#deleteUser(edu.uwm.cs757.model.User)
     */
    @Override
    public boolean deleteUser(final String userName)
    {

        // connection
        Connection dbConnection = null;
        try
        {
            // obtain a connection from factory
            final DBConnectionFactory connectionFactory = DBConnectionFactory.getInstance();
            dbConnection = connectionFactory.getDBConnection();
            dbConnection.setAutoCommit(false);
            
            // delete user from users table
            final DeleteUserCommand dbUserCommand = new DeleteUserCommand(userName);
            final int deletedUserCount = dbUserCommand.executeCommand(dbConnection);

            if (deletedUserCount > 0)
            {
                // if there were records deleted.. drop the db user as well
                final DropDBUserCommand dropUserCommand = new DropDBUserCommand(userName);
                dropUserCommand.executeCommand(dbConnection);
            }

            dbConnection.commit();

            return true;

        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        catch (final SQLException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        finally
        {
            if (dbConnection != null)
            {
                try
                {
                    dbConnection.close();
                }
                catch (final SQLException e)
                {
                    // DO NOTHING
                    e.printStackTrace();
                }
            }
        }

    }

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.service.user.UserService#searchUserByName(java.lang.String)
     */
    @Override
    public User searchUserByID(final String userName)
    {
        if (userName == null)
        {
            return null;
        }

        try
        {
            final SearchUserByIDCommand searchCommand = new SearchUserByIDCommand(userName);
            return searchCommand.executeCommand();
        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            return null;
        }
    }

 
	@Override
	public List<User> getAllUsers() {
        try
        {
            final GetAllUsersCommand Command = new GetAllUsersCommand();
            return Command.executeCommand();
        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            return null;
        }
	}
	
	public static void main(String[] args)
    {
        UserService service = new UserServiceImpl();
        service.createUser(new User("shyamnair", Role.ADMINISTRATOR),"Welcome01");
    }

}

/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 9, 2014   Shyam Nair
 * File created
 * ----------------------------------------------------------------------------
 * Apr 21, 2014   Shyam Nair
 * Added Logic For Grant privilege
 * ----------------------------------------------------------------------------
 */